﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MoroMoro.Samples.ToDoList.Domain.Categories;

namespace MoroMoro.Samples.ToDoList.Presentation.Views.ToDos
{
    /// <summary>
    /// ToDo 構築フォームです。
    /// </summary>
    public partial class ToDoBuildForm : BuildForm, IToDoBuildView
    {
        /// <summary>
        /// <see cref="ToDoBuildForm"/> クラスの新しいインスタンスを初期化します。
        /// </summary>
        public ToDoBuildForm()
        {
            InitializeComponent();
        }


        #region Events

        /// <inheritdoc/>
        event EventHandler IToDoBuildView.TitleTextChanged
        {
            add
            {
                _titleTextBox.TextChanged += value;
            }
            remove
            {
                _titleTextBox.TextChanged -= value;
            }
        }

        /// <inheritdoc/>
        event EventHandler IToDoBuildView.SelectedCategoryChanged
        {
            add
            {
                _categoryComboBox.SelectedIndexChanged += value;
            }
            remove
            {
                _categoryComboBox.SelectedIndexChanged -= value;
            }
        }

        /// <inheritdoc/>
        event EventHandler IToDoBuildView.MemoAdditionButtonClick
        {
            add
            {
                _memoAdditionButton.Click += value;
            }
            remove
            {
                _memoAdditionButton.Click -= value;
            }
        }

        /// <inheritdoc/>
        event EventHandler IToDoBuildView.MemoRemoveButtonClick
        {
            add
            {
                _memoRemoveButton.Click += value;
            }
            remove
            {
                _memoRemoveButton.Click -= value;
            }
        }

        #endregion

        #region Properties

        /// <inheritdoc/>
        string IToDoBuildView.Title
        {
            get
            {
                return _titleTextBox.Text;
            }
            set
            {
                _titleTextBox.Text = value;
            }
        }

        /// <inheritdoc/>
        int IToDoBuildView.SelectedMemoTabPageIndex
        {
            get
            {
                return _memoListTabControl.SelectedIndex;
            }
            set
            {
                _memoListTabControl.SelectedIndex = value;
            }
        }

        /// <inheritdoc/>
        int IToDoBuildView.MemoTabPagesCount
        {
            get
            {
                return _memoListTabControl.TabPages.Count;
            }
        }

        /// <inheritdoc/>
        Category IToDoBuildView.Category
        {
            get
            {
                return (Category)_categoryComboBox.SelectedValue;
            }
            set
            {
                if (value == null)
                {
                    _categoryComboBox.SelectedIndex = 0;
                }
                else
                {
                    _categoryComboBox.SelectedValue = value;
                }
            }
        }

        #endregion

        #region Methods

        /// <inheritdoc/>
        void IToDoBuildView.BindCategories(object dataSource, string valueMember, string displayMember)
        {
            _categoryComboBox.ValueMember = valueMember;
            _categoryComboBox.DisplayMember = displayMember;
            _categoryComboBox.DataSource = dataSource;
        }

        /// <inheritdoc/>
        string IToDoBuildView.GetMemoText(int memoIndex)
        {
            var memoTabPage = (MemoTabPage)_memoListTabControl.TabPages[memoIndex];
            return memoTabPage.TextBox.Text;
        }

        /// <inheritdoc/>
        void IToDoBuildView.SetMemoText(int memoIndex, string value)
        {
            var memoTabPage = (MemoTabPage)_memoListTabControl.TabPages[memoIndex];
            memoTabPage.TextBox.Text = value;
        }

        /// <inheritdoc/>
        void IToDoBuildView.AddMemoArea(EventHandler<MemoEventArgs> textChangedEventHandler)
        {
            _memoListTabControl.SuspendLayout();
            try
            {
                var memoTabPage = new MemoTabPage();
                memoTabPage.TextBox.TextChanged += (sender, args) =>
                {
                    var index = _memoListTabControl.TabPages.IndexOf(memoTabPage);
                    textChangedEventHandler(memoTabPage.TextBox, new MemoEventArgs(index));
                };
                _memoListTabControl.TabPages.Add(memoTabPage);
            }
            finally
            {
                _memoListTabControl.ResumeLayout();
            }
        }

        /// <inheritdoc/>
        void IToDoBuildView.RemoveMemoArea(int memoIndex)
        {
            _memoListTabControl.SuspendLayout();
            try
            {
                _memoListTabControl.TabPages.RemoveAt(memoIndex);
            }
            finally
            {
                _memoListTabControl.ResumeLayout();
            }
        }

        /// <inheritdoc/>
        void IToDoBuildView.SetTitleError(string message)
        {
            _errorProvider.SetError(_titleTextBox, message);
        }

        /// <inheritdoc/>
        void IToDoBuildView.SetMemoListError(string message)
        {
            _errorProvider.SetError(_memoListTabControl, message);
        }

        /// <inheritdoc/>
        void IToDoBuildView.SetMemoTextError(int memoIndex, string message)
        {
            var memoTabPage = (MemoTabPage)_memoListTabControl.TabPages[memoIndex];
            _errorProvider.SetError(memoTabPage.TextBox, message);
        }

        #endregion
    }
}
